#!/usr/bin/python

import unittest

from hips.scheduling import constraint
from hips.scheduling import doctor
from hips.scheduling import problem
from hips.scheduling import schedule_mocks


class SchedulingTest(unittest.TestCase):
  """Functional test of the scheduling system."""

  def testSchedulingSolver(self):
    d1 = doctor.Doctor(
        "McCoy",
        days_off=[5, 6],
        min_shifts=12,
        max_shifts=15,
        max_night_shifts=5,
        max_consecutive_days=5)
    d2 = doctor.Doctor(
        "Crusher",
        days_off=[7, 8],
        min_shifts=12,
        max_shifts=15,
        max_night_shifts=5,
        max_consecutive_days=5)
    d3 = doctor.Doctor(
        "Selar",
        days_off=[1, 2],
        min_shifts=5,
        max_shifts=10,
        max_night_shifts=3,
        max_consecutive_days=5)
    d4 = doctor.Doctor(
        "Bashir",
        days_off=[3, 4, 10, 11],
        min_shifts=12,
        max_shifts=15,
        max_night_shifts=5,
        max_consecutive_days=5)
    d5 = doctor.Doctor(
        "Zimmerman",
        days_off=[1, 2, 3, 4, 5, 6, 20, 25],
        min_shifts=12,
        max_shifts=15,
        max_night_shifts=5,
        max_consecutive_days=5)
    d6 = doctor.Doctor(
        "Chapel",
        days_off=[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
        min_shifts=12,
        max_shifts=15,
        max_night_shifts=5,
        max_consecutive_days=5)
    d7 = doctor.Doctor(
        "Quaice",
        days_off=[11, 12, 13, 14, 15],
        min_shifts=12,
        max_shifts=15,
        max_night_shifts=5,
        max_consecutive_days=4)

    mock_schedule = schedule_mocks.CreateMockSchedule(days=30)
    scheduling_problem = problem.SchedulingProblem(mock_schedule)
    scheduling_problem.AddDoctor(d1)
    scheduling_problem.AddDoctor(d2)
    scheduling_problem.AddDoctor(d3)
    scheduling_problem.AddDoctor(d4)
    scheduling_problem.AddDoctor(d5)
    scheduling_problem.AddDoctor(d6)
    scheduling_problem.AddDoctor(d7)

    solution = scheduling_problem.Solve(steps=100)
    self.assertTrue("No solution found", solution)
    log = scheduling_problem.GetSolutionLog()
    self.assertTrue("Log not created", log)
    print
    print "Final solution:"
    if solution:
      print solution.FormatSolutionAsString()
      print
      print "SUCCESS"
    else:
      print "No solutions found"
    print
    print "Iterations: %d" % scheduling_problem.iterations
    print
    print log.Conflicts()

if __name__ == "__main__":
  unittest.main()
